package org.shiro.util;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/** @author 党怡承 */
@Component
public class FileUtils {
  /**
   * 上传文件
   *
   * @param file
   * @return
   */
  public static File uploadFile(MultipartFile file) {
    String originalFileName = file.getOriginalFilename();
    String fileSuffix = originalFileName.substring(originalFileName.lastIndexOf("."));
    if (".jpg".equalsIgnoreCase(fileSuffix)
        || ".jpeg".equalsIgnoreCase(fileSuffix)
        || ".png".equalsIgnoreCase(fileSuffix)) {
      String realPath = System.getProperty("user.dir");
      String directory = "uploadFile";
      File files = new File(realPath, directory);
      if (!files.exists()) {
        files.mkdirs();
      }
      String targetFile = files.getAbsolutePath();
      String newFileName = UUIDUtils.getUUID() + fileSuffix;
      File newFile = new File(targetFile, newFileName);
      try {
        file.transferTo(newFile);
        return newFile;
      } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
      }
    } else {
      throw new IllegalArgumentException("上传图片格式错误");
    }
  }

  /**
   * 删除文件
   *
   * @param filePath
   * @return
   */
  public static boolean removeFile(String filePath) {
    File file = new File(filePath);
    if (file.isFile() && file.exists()) {
      file.delete();
      return true;
    }
    return false;
  }

  /**
   * 根据路径删除包含指定字符的文件
   *
   * @param filePath
   * @param keywords
   * @return
   */
  public static boolean deleteFile(String filePath, String keywords) {
    File file = new File(filePath);
    if (!file.exists()) {
      return false;
    }
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++) {
      File temp = files[i];
      if (temp.getName().equalsIgnoreCase(keywords)) {
        temp.delete();
      }
    }
    return true;
  }

  /**
   * 复制文件
   *
   * @param sourceFile
   * @param targetFile
   * @param newFileName
   * @return
   * @throws Exception
   */
  public static boolean copyFile(File sourceFile, File targetFile, String newFileName)
      throws Exception {
    if (!sourceFile.exists()) {
      throw new IllegalArgumentException("源文件不存在");
    } else if (!targetFile.exists()) {
      throw new IllegalArgumentException("目标文件不存在");
    } else if (newFileName == null) {
      throw new IllegalArgumentException("新建文件名称为空");
    } else {
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
      BufferedOutputStream bos =
          new BufferedOutputStream(new FileOutputStream(new File(targetFile, newFileName)));
      byte[] bytes = new byte[4096];
      int size = 0;
      while ((size = bis.read()) != -1) {
        bos.write(bytes, 0, size);
      }
      bos.flush();
      bis.close();
      bos.close();
    }
    return true;
  }
}
